package com.own.demo.lwf.utils.workUtils;

import java.math.BigDecimal;

public class MoneyUtil {

    public static final String HanDigiStr[] = new String[]{"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};

    public static final String HanDiviStr[] = new String[]{"","拾","佰","仟","万","拾","佰","仟","亿",
            "拾","佰","仟","万","拾","佰","仟","亿",
            "拾","佰","仟","万","拾","佰","仟" };

    public static String positiveIntegerToHanStr(String NumStr) {
        // 输入字符串必须正整数，只允许前导空格(必须右对齐)，不宜有前导零
        StringBuffer rMBStr = new StringBuffer("");
        boolean lastzero = false;
        boolean hasvalue = false; // 亿、万进位前有数值标记
        int len, n;
        len = NumStr.length();
        if( len > 15 )
            return "数值过大!";
        for(int i=len-1;i>=0;i--) {
            if( NumStr.charAt(len-i-1)==' ' ) continue;
            n = NumStr.charAt(len-i-1) - '0';
            if( n<0 || n>9 ) return "输入含非数字字符!";
            if( n!=0 ) {
                if( lastzero ) rMBStr = rMBStr.append(HanDigiStr[0]);  // 若干零后若跟非零值，只显示一个零
                // 除了亿万前的零不带到后面
                //if( !( n==1 && (i%4)==1 && (lastzero || i==len-1) ) )    // 如十进位前有零也不发壹音用此行
                if( !( n==1 && (i%4)==1 && i==len-1 ) ) {     // 十进位处于第一位不发壹音
                    rMBStr = rMBStr.append(HanDigiStr[n]);
                    rMBStr = rMBStr.append(HanDiviStr[i]);    // 非零值后加进位，个位为空
                    hasvalue = true;                                    // 置万进位前有值标记
                }
            }else{
                if( (i%8)==0 || ((i%8)==4 && hasvalue) )  // 亿万之间必须有非零值方显示万
                    rMBStr = rMBStr.append(HanDiviStr[i]);   // “亿”或“万”
            }
            if( i%8==0 ) hasvalue = false ;      // 万进位前有值标记逢亿复位
            lastzero = (n==0) && (i%4!=0);
        }
        if( rMBStr.length()==0 ) return HanDigiStr[0];         // 输入空字符或"0"，返回"零"
        return rMBStr.toString();
    }

    /**
     * BigDecimal转换成大写金额
     * @param val
     * @param flag 是否带￥符号  true带，false不带
     * @return
     */
    public static String numToRMBStr(BigDecimal val, boolean flag){
        return numToRMBStr(val.doubleValue(), flag);
    }

    /**
     * Double类型转换成大写金额
     * @param val
     * @param flag 是否带￥符号  true带，false不带
     * @return
     */
    public static String numToRMBStr(double val, boolean flag){
        String signStr ="" ;
        String TailStr ="";
        long  fraction, integer;
        int jiao,fen;
        if( val<0 ) {
            val = -val;
            signStr = "负";
        }
        if(val > 99999999999999.999 || val <-99999999999999.999 ) return "数值位数过大!";
        // 四舍五入到分
        long temp = Math.round(val*100);
        integer = temp/100;
        fraction = temp%100;
        jiao = (int)fraction/10;
        fen = (int)fraction%10;
        if( jiao==0 && fen==0 ) {
            TailStr = "整";
        }
        else {
            TailStr = HanDigiStr[jiao];
            if( jiao!=0 )
                TailStr += "角";
            if( integer==0 && jiao==0 )
                TailStr = "";
            if( fen!=0 )
                TailStr += HanDigiStr[fen] + "分";
        }
        String strMoney = signStr+positiveIntegerToHanStr(String.valueOf(integer) )+"元"+TailStr;
        return flag ? "￥" + strMoney : strMoney;
    }

}
